//
//  HZMAPImanager.m
//  Mpos
//
//  Created by huazi on 14-10-21.
//  Copyright (c) 2014年 huifu. All rights reserved.
//

#import "HZMAPImanager.h"
@interface HZMAPImanager()
@property (nonatomic, strong) NSHashTable *delegates;
@end

@implementation HZMAPImanager
+(instancetype)shareMAPImanager
{
    static HZMAPImanager *_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _manager = [[HZMAPImanager alloc] init];
        
    });
    return _manager;
}
-(id)init
{
    if (self)
    {
        self.delegates =[[NSHashTable alloc] init];
    }
    return self;
}
-(void)addDelegate:(id<HZMAPIManagerDelegate>)delegate
{
    @synchronized(self)
    {
        if (delegate && ![self.delegates containsObject:delegate]) {
            [self.delegates addObject:delegate];
        }
    }
}
-(void)removeDelegate:(id<HZMAPIManagerDelegate>)delegate
{
    @synchronized(self)
    {
        if (delegate && [self.delegates containsObject:delegate]) {
            [self.delegates removeObject:delegate];
        }
    }

}

#pragma mark - Private Help Method
- (void)postWithWSRequest:(HZMRequest *)wxRequest
{
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",SERVICE_URL,wxRequest.requsetId];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    manager.requestSerializer =[HZHTTPRequestSerializer serializer];
    manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    NSDictionary *dic =wxRequest.requestParamDic;
//    NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:wxRequest.requsetId,@"TXNCODE",[NSJSONSerialization returnJsonStrWithObject:wxRequest.requestParamDic],@"reqStr", nil];
    [manager POST:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject)
    {
        NSString *str =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        HZMResponse *response = [[HZMResponse alloc] init];
        response.requestId = wxRequest.requsetId;
        response.responseData =[NSJSONSerialization returnObjectWithJsonStr:str];
        response.callBackDelegate =wxRequest.callBackDelegate;
        if ([[[response.responseData objectForKey:@"head"] objectForKey:@"code"] isEqualToString:@"200"])
        {
            [response.callBackDelegate transactionFinished:response];
            
        }
        else
        {
            
            response.responseMsg =[[response.responseData objectForKey:@"head"] objectForKey:@"msg"];
            if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
            {
                if ([NSString isEmpty:str])
                {
                    response.responseMsg =@"字符中包含非法字符";
                    [response.callBackDelegate transactionFailed:response];
                }
                else
                {
                    [response.callBackDelegate transactionFailed:response];
                }
                
            }
            
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"%@",error.description);
        //DDLogInfo(@"%@", error);
        HZMResponse *response = [[HZMResponse alloc] init];
        response.requestId = wxRequest.requsetId;
        response.responseMsg =@"请求超时，请确定您的网络";
        response.callBackDelegate =wxRequest.callBackDelegate;
        if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
        {
            [response.callBackDelegate transactionFailed:response];
        }
        
    }];
}
- (void)postWithWSRequestWithFile:(HZMRequest *)wxRequest
{
//    NSString *urlStr = [NSString stringWithFormat:@"%@",SERVICE_FILE_URL];
//    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
//    manager.requestSerializer =[HZHTTPRequestSerializer serializer];
//    manager.responseSerializer =[AFHTTPResponseSerializer serializer];
////    NSData *data =[NSData dataWithContentsOfURL:[NSURL URLWithString:wxRequest.filePath]];
//    NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:wxRequest.requsetId,@"TXNCODE",[NSJSONSerialization returnJsonStrWithObject:wxRequest.requestParamDic],@"reqStr", nil];
//    [manager POST:urlStr parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        [formData appendPartWithFileURL:[NSURL fileURLWithPath:wxRequest.filePath] name:@"opSendf" error:nil];
////        [formData appendPartWithFormData:data name:@"opSendf"];
//        
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *str =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        HZMResponse *response = [[HZMResponse alloc] init];
//        response.requestId = wxRequest.requsetId;
//        response.responseData =[NSJSONSerialization returnObjectWithJsonStr:str];
//        response.callBackDelegate =wxRequest.callBackDelegate;
//        if ([[response.responseData objectForKey:@"rescode"] isEqualToString:@"0"])
//        {
//            [response.callBackDelegate transactionFinished:response];
//            
//        }
//        else
//        {
//            
//            response.responseMsg =[response.responseData objectForKey:@"resdes"];
//            if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
//            {
//                if ([NSString isEmpty:str])
//                {
//                    response.responseMsg =@"字符中包含非法字符";
//                    [response.callBackDelegate transactionFailed:response];
//                }
//                else
//                {
//                    [response.callBackDelegate transactionFailed:response];
//                }
//                
//            }
//            
//        }
//        
//
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        DLog(@"%@",error.description);
//        //DDLogInfo(@"%@", error);
//        HZMResponse *response = [[HZMResponse alloc] init];
//        response.requestId = wxRequest.requsetId;
//        response.responseMsg =@"请求超时，请确定您的网络";
//        response.callBackDelegate =wxRequest.callBackDelegate;
//        if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
//        {
//            [response.callBackDelegate transactionFailed:response];
//        }
//
//    }];
}
- (void)getWithWSRequest:(HZMRequest *)wxRequest {
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",SERVICE_URL,wxRequest.requsetId];
    AFHTTPRequestOperationManager *manager =[AFHTTPRequestOperationManager manager];
    manager.requestSerializer =[HZHTTPRequestSerializer serializer];
    manager.responseSerializer =[AFHTTPResponseSerializer serializer];
    NSDictionary *dic =wxRequest.requestParamDic;
    [manager GET:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *str =[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        HZMResponse *response = [[HZMResponse alloc] init];
        response.requestId = wxRequest.requsetId;
        response.responseData =[NSJSONSerialization returnObjectWithJsonStr:str];
        response.callBackDelegate =wxRequest.callBackDelegate;
        if ([[[response.responseData objectForKey:@"head"] objectForKey:@"code"] isEqualToString:@"200"])
        {
            [response.callBackDelegate transactionFinished:response];
            
        }
        else
        {
            
            response.responseMsg =[[response.responseData objectForKey:@"head"] objectForKey:@"msg"];
            if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
            {
                if ([NSString isEmpty:str])
                {
                    response.responseMsg =@"字符中包含非法字符";
                    [response.callBackDelegate transactionFailed:response];
                }
                else
                {
                    [response.callBackDelegate transactionFailed:response];
                }
                
            }
            
        }

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        DLog(@"%@",error.description);
        //DDLogInfo(@"%@", error);
        HZMResponse *response = [[HZMResponse alloc] init];
        response.requestId = wxRequest.requsetId;
        response.responseMsg =@"请求超时，请确定您的网络";
        response.callBackDelegate =wxRequest.callBackDelegate;
        if ([response.callBackDelegate respondsToSelector:@selector(transactionFailed:)])
        {
            [response.callBackDelegate transactionFailed:response];
        }

    }];
    //    NSDictionary *dic =[NSDictionary dictionaryWithObjectsAndKeys:wxRequest.requsetId,@"TXNCODE",[NSJSONSerialization returnJsonStrWithObject:wxRequest.requestParamDic],@"reqStr", nil];
//    [manager POST:urlStr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject)
//     {
//             } failure:^(AFHTTPRequestOperation *operation, NSError *error)
//     {
//         
//     }];

}
@end
